package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public abstract class BaseTeacherController {
    TeacherService teacherService=new TeacherService();
    Scanner sc=new Scanner(System.in);
    public final void begin() {

        looper:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int number=sc.nextInt();
            switch(number){
                case 1:
                    addTeacher();
                    break;
                case 2:
                    removeTeacher();
                    break;
                case 3:
                    changeTeacher();
                    break;
                case 4:
                    watchTeacher();
                    break;
                case 5:
                    System.out.println("欢迎下次使用教师信息管理系统，再见！");
                    break looper;
                default:
                    System.out.println("输入有误，请重新输入：");
                    break;
            }
        }
    }

    public final void changeTeacher() {
        Teacher[] teach=teacherService.watch();
        //首先判断数组是否为空，不是空，执行以下操作
        if (teach==null){
            System.out.println("查无信息，请先添加");
            return;
        }
        String id=null;
        while (true) {
            System.out.println("请输入需要修改的工号：");
            id=sc.next();
            boolean tea=teacherService.query(id);
            if (tea){
                System.out.println("输入工号不存在，请重新输入");
            }else{
                break;
            }
        }
        System.out.println("请输入需要修改的姓名：");
        String name =sc.next();
        System.out.println("请输入需要修改的年龄：");
        String age =sc.next();
        System.out.println("请输入需要修改的生日：");
        String birth =sc.next();
        boolean teacher=teacherService.change(id,name,age,birth);
        if (teacher){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }

    public final void removeTeacher() {
        Teacher[] teach=teacherService.watch();
        //首先判断数组是否为空，不是空，执行以下操作
        if (teach==null){
            System.out.println("查无信息，请先添加");
            return;
        }
        String id=null;
        while (true) {
            System.out.println("请输入需要删除的工号：");
            id=sc.next();
            boolean tea=teacherService.query(id);
            if (tea){
                System.out.println("输入工号不存在，请重新输入");
            }else{
                break;
            }
        }
        boolean teachers=teacherService.delete(id);
        if (teachers){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

    public final void watchTeacher() {
        Teacher[] teach=teacherService.watch();
        //首先判断数组是否为空，不是空，执行以下操作
        if (teach==null){
            System.out.println("查无信息，请先添加");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teach.length; i++) {
            if (teach[i]!=null){
                System.out.println(teach[i].getID()+"\t"+teach[i].getName()+"\t"+teach[i].getAge()+"\t\t"+teach[i].getBirthday());
            }
        }
    }

    public abstract void addTeacher();
}
